package com.nariano.webapp;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import com.nariano.server.domain.entities.City;
import com.nariano.server.domain.entities.PopulationType;
import com.nariano.server.domain.utils.PopulationTypeList;
import com.nariano.server.services.cityservice.CityService;
import com.nariano.server.utils.spring.SpringFactory;

public class CityPanel extends Panel {
	
	public CityPanel(String id, final City city) {
		super(id);
		
		
		Form form = new StatelessForm("cityForm");
		add(form);
		
		String name = city==null?"":city.getName();
		form.add(new Label("cityName", name));
		
		IChoiceRenderer render = getRender();
		IModel model = city==null ? getNullModel() : new PropertyModel(city,"createType");
		
		form.add(new DropDownChoice("createType", model , Arrays.asList(PopulationType.values()), render ));
		
		form.add(new Button("submit", new ResourceModel("submitText")) {

			@Override
			public void onSubmit() {
				CityService service = SpringFactory.getBean("cityService", CityService.class);
				service.updateCity(city);
				
				super.onSubmit();
			}
			
		});
	}

	private IModel getNullModel() {
		return new IModel() {

			@Override
			public Object getObject() {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void setObject(Object object) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void detach() {
				// TODO Auto-generated method stub
				
			}
		};
	}

	private IChoiceRenderer getRender() {
		return new IChoiceRenderer<PopulationType>() {

			@Override
			public Object getDisplayValue(PopulationType type) {
				return type.getName();
			}

			@Override
			public String getIdValue(PopulationType type, int index) {
				return type.getId();
			}
		};
	}

}
